import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import '@/assets/global.less'
import ElementUI, { Message } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import '../node_modules/echarts/dist/echarts.js'
import VueQuillEditor from 'vue-quill-editor'

// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
Vue.use(ElementUI)

Vue.use(VueQuillEditor /* { default global options } */)
Vue.prototype.$axios = axios
axios.defaults.baseURL = 'http://www.liulongbin.top:3008'
Vue.config.productionTip = false

axios.interceptors.request.use(function (config) {
  // console.log(config)
  // console.log(store.state.user)
  const token = store.state.user.token
  if (token) {
    config.headers.Authorization = token
  }
  return config
}, function (error) {
  // Do something with request error
  return Promise.reject(error)
})

axios.interceptors.response.use(function (response) {
  // console.log(response)
  // 响应成功, 将 tokenFlag 置为 0
  store.commit('user/updateTokenFlag', 0)
  return response
}, function (error) {
  // console.log(error)

  if (error.response.status === 401) {
    //  清空token
    store.commit('user/updataToken', '')
    // 跳转到登陆页
    router.push('/login')
    // 提醒用户 身份认证失败
    // Message.error('身份认证失败')
    // 响应失败,判断是否为第一次弹出警告,为0则是第一次弹出警告
    if (store.state.user.tokenFlag === 0) {
      Message.error('身份认证失败')
      // 第一次弹出警告后, 将 tokenFlag 置为 1
      store.commit('user/updateTokenFlag', 1)
    }
  }

  return Promise.reject(error)
})

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
